﻿using Joy.Erp.BasicArchives.Localization;
using System.Linq;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Localization;

namespace Joy.Erp.Ufida.Permissions
{
    public class UfidaPermissionDefinitionProvider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(UfidaPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(UfidaPermissions.Menus, L("Permission:Menus"));
            }

            var administrationMenu = menus.GetPermissionOrNull(UfidaPermissions.AdministrationMenu.Default);
            if (administrationMenu == null)
            {
                administrationMenu = menus.AddPermission(UfidaPermissions.AdministrationMenu.Default, L("Permission:Menus.Administration"));
            }

            var syncMenu = administrationMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.AdministrationMenu.Sync);
            if (syncMenu == null)
            {
                syncMenu = administrationMenu.AddChild(UfidaPermissions.AdministrationMenu.Sync, L("Permission:Menus.Administration.Sync"));
            }

            var archivesSyncMenu = syncMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.AdministrationMenu.ArchivesSync);
            if (archivesSyncMenu == null)
            {
                archivesSyncMenu = syncMenu.AddChild(UfidaPermissions.AdministrationMenu.ArchivesSync, L("Permission:Menus.Administration.Sync.ArchivesSync"));
            }

            var documentSyncMenu = syncMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.AdministrationMenu.DocumentSync);
            if (documentSyncMenu == null)
            {
                documentSyncMenu = syncMenu.AddChild(UfidaPermissions.AdministrationMenu.DocumentSync, L("Permission:Menus.Administration.Sync.DocumentSync"));
            }
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
}
